package com.domel76;

import java.util.ArrayList;

import com.domel76.quickfilm.beans.CinemaBean;
import com.domel76.quickfilm.common.Constans;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Cinema extends ListActivity {
	ArrayList<String> cinemas = new ArrayList<String>();
	ArrayList<CinemaBean> cinemasBean = new ArrayList<CinemaBean>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SQLiteDatabase baza = null;

		try {

			baza = this.openOrCreateDatabase(Constans.DB_NAME, MODE_PRIVATE, null);
			Cursor cursor = baza.rawQuery("SELECT id, name  FROM Cinema order by name", null);

			if (cursor.moveToFirst()) {
				do {
					String name = cursor.getString(cursor.getColumnIndex("name"));
					int id = cursor.getInt(cursor.getColumnIndex("id"));
					cinemas.add(name);
					cinemasBean.add(new CinemaBean(id, name));

				} while (cursor.moveToNext());
			}

			this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cinemas));

		} catch (SQLiteException e) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} finally {
			if (baza != null)
				baza.close();
		}
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

			}
		});

		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Intent intent = new Intent(getApplicationContext(), Seans.class);
				intent.putExtra("cinemaId", cinemasBean.get(arg2).id);
				startActivityForResult(intent, 1338);

			}
		});
	}
}